<template>
  <div style="width: 100%; height: 100%">
    <div style="width: 100%; text-align: center; margin-bottom: 10px">
      <el-radio-group size="small" v-model="radio">
        <el-radio-button label="1">按地区</el-radio-button>
        <el-radio-button label="2">按行业</el-radio-button>
      </el-radio-group>
    </div>
    <el-input placeholder="输入关键字进行过滤" v-model="filterText"> </el-input>
    <el-tree
      v-loading="loading"
      :data="treeData"
      show-checkbox
      node-key="companyNo"
      :filter-node-method="filterNode"
      :props="defaultProps"
      ref="tree"
      @check-change="checkChange"
    >
    </el-tree>
  </div>
</template>

<script>
import {
  getAreaTreeListApi,
  getIndustryTreeListApi
} from '@/api/Intelligence-maintenance/Auxiliary-analysis/index'
export default {
  data() {
    return {
      radio: '1',
      loading: false,
      filterText: '',
      treeData: [],
      defaultProps: {
        children: 'childrenList',
        label: 'name'
      }
    }
  },
  watch: {
    radio: {
      async handler(newVal) {
        this.loading = true
        const res =
          newVal == '1'
            ? await getAreaTreeListApi()
            : await getIndustryTreeListApi()
        console.log(res)
        this.treeData = res.data.entity.childrenList
        this.checkItem = res.data.entity.childrenList[0]
        this.loading = false
      },
      immediate: true
    },
    filterText(val) {
      this.$refs.tree.filter(val)
    }
  },
  methods: {
    filterNode(value, data) {
      if (!value) return true
      return data.name.indexOf(value) !== -1
    },
    checkChange(a, b, c) {
      // console.log(a, b, c)
      const tree = this.$refs.tree.getCheckedKeys(true)
      const node = this.$refs.tree.getCheckedNodes(true)
      this.companyNos = tree.join(',')
      this.$emit('checkChange', this.companyNos, node)
    }
  }
}
</script>

<style lang="scss" scoped></style>
